define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'community/teacher/index' + location.search,
                    add_url: 'community/teacher/add',
                    edit_url: 'community/teacher/edit',
                    del_url: 'community/teacher/del',
                    multi_url: 'community/teacher/multi',
                    import_url: 'community/teacher/import',
                    table: 'teacher',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID'), sortable: true},
                        {field: 'name', title: __('姓名'), operate: 'LIKE'},
                        {field: 'gender', title: __('性别'), searchList: {"1":__('男'),"2":__('女')}, formatter: Table.api.formatter.normal},
                        {field: 'avatar', title: __('头像'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'phone', title: __('联系电话'), operate: 'LIKE'},
                        {field: 'specialty', title: __('专业特长'), operate: 'LIKE'},
                        {field: 'teaching_years', title: __('教龄'), operate: 'BETWEEN', sortable: true},
                        {field: 'community.name', title: __('所属社区'), operate: 'LIKE'},
                        {field: 'status', title: __('状态'), searchList: {"0":__('禁用'),"1":__('正常')}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            
            // 初始化上传组件
            $(".faupload").each(function () {
                var id = $(this).attr("id");
                var inputId = $(this).data("input-id") ? $(this).data("input-id") : "";
                var previewId = $(this).data("preview-id") ? $(this).data("preview-id") : "";
                var previewUrl = $(this).data("preview-url") ? $(this).data("preview-url") : "";
                var params = $(this).data("params") ? $(this).data("params") : '';
                
                Upload.api.plupload("#" + id, function (data, ret) {
                    if (inputId) {
                        $("#" + inputId).val(data.url).trigger("change");
                    }
                    if (previewId && data.url) {
                        var value = data.url;
                        var preview = $("#" + previewId);
                        var type = typeof value;
                        var url = (type === 'object' ? value.url : value);
                        url = url ? url : '/assets/img/blank.gif';
                        preview.empty().append('<li><img src="' + Fast.api.cdnurl(url) + '" class="img-responsive"></li>');
                    }
                    Layer.msg(__('Upload successful'));
                }, function (up, ret) {
                    Layer.msg(__('Upload failed'), {icon: 2});
                }, {
                    multipart_params: params
                });
            });
        },
        edit: function () {
            Controller.api.bindevent();
            
            // 初始化上传组件
            $(".faupload").each(function () {
                var id = $(this).attr("id");
                var inputId = $(this).data("input-id") ? $(this).data("input-id") : "";
                var previewId = $(this).data("preview-id") ? $(this).data("preview-id") : "";
                var previewUrl = $(this).data("preview-url") ? $(this).data("preview-url") : "";
                var params = $(this).data("params") ? $(this).data("params") : '';
                
                Upload.api.plupload("#" + id, function (data, ret) {
                    if (inputId) {
                        $("#" + inputId).val(data.url).trigger("change");
                    }
                    if (previewId && data.url) {
                        var value = data.url;
                        var preview = $("#" + previewId);
                        var type = typeof value;
                        var url = (type === 'object' ? value.url : value);
                        url = url ? url : '/assets/img/blank.gif';
                        preview.empty().append('<li><img src="' + Fast.api.cdnurl(url) + '" class="img-responsive"></li>');
                    }
                    Layer.msg(__('Upload successful'));
                }, function (up, ret) {
                    Layer.msg(__('Upload failed'), {icon: 2});
                }, {
                    multipart_params: params
                });
            });
            
            // 初始化预览图片
            $(".faupload-preview").each(function () {
                var id = $(this).attr("id");
                var inputId = id.replace("p-", "c-");
                var value = $("#" + inputId).val();
                if (value) {
                    var preview = $("#" + id);
                    var type = typeof value;
                    var url = (type === 'object' ? value.url : value);
                    url = url ? url : '/assets/img/blank.gif';
                    preview.empty().append('<li><img src="' + Fast.api.cdnurl(url) + '" class="img-responsive"></li>');
                }
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                
                // 初始化富文本编辑器
                $(".editor").each(function () {
                    var id = $(this).attr("id");
                    require(['summernote'], function () {
                        $('#' + id).summernote({
                            height: 250,
                            lang: 'zh-CN',
                            fontNames: [
                                'Arial', 'Arial Black', 'Courier New', 'Helvetica Neue', 'Helvetica', 'Impact', 'Lucida Grande',
                                'Tahoma', 'Times New Roman', 'Verdana', 'Microsoft YaHei', 'Songti SC', 'SimSun'
                            ],
                            fontNamesIgnoreCheck: [
                                'Microsoft YaHei', 'Songti SC', 'SimSun'
                            ],
                            toolbar: [
                                ['style', ['style', 'bold', 'italic', 'underline', 'strikethrough', 'clear']],
                                ['font', ['fontname', 'fontsize']],
                                ['color', ['color']],
                                ['para', ['ul', 'ol', 'paragraph', 'height']],
                                ['table', ['table']],
                                ['insert', ['link', 'picture', 'video']],
                                ['view', ['fullscreen', 'codeview', 'help']]
                            ],
                            callbacks: {
                                onChange: function (contents) {
                                    $(this).val(contents);
                                    $(this).trigger('change');
                                },
                                onInit: function () {
                                }
                            }
                        });
                    });
                });
            }
        }
    };
    return Controller;
});